<template>
  <view class="invoiceList">
    <headerSearchInvoice :query="query" @queryList="queryList" @search="search"></headerSearchInvoice>
    <scroll-view
        :scroll-y="true"
        @scrolltolower="handleScroll"
        v-if="showList.length>0"
        class="lists"
    >
      <template v-for="(item, index ) in showList">
        <list-item
            :item="item"
            :index="index"
            @handleReverse="handleReverse"
            @handleCancel="handleCancel"
        ></list-item>
      </template>
    </scroll-view>
    <view class="container_empty" v-else>
      <image mode="aspectFit" src="../../assets/images/index/empty3.png"></image>
      <view class="empty_text">暂无信息</view>
    </view>

    <drawer :show="showReverse" :show-border="false" :title="'冲红申请'" @close="closeReverse(false)" :show-close="true">
      <view class="reverse">
        <nut-textarea v-model="reverseReason" limit-show max-length="100" placeholder="请填写冲红原因，待审核后可冲红哦~" class="reverse_textarea" />
      </view>
      <view class="save">
        <view class="btn" @click="saveReverse">提交申请</view>
      </view>
    </drawer>
  </view>
</template>
<script lang="ts" setup>
import { computed, onMounted, ref, watch } from "vue";
// import { OrderListType } from "../orderTracking/listType";
import { changeList, $post } from "@/@api";
import listItem from "@/pages/invoicing/invoiceList.vue";
import headerSearchInvoice from './headerSearchInvoice.vue'
import Taro, { useDidShow } from "@tarojs/taro";
import useUserInfoStore from "../../store/user";
import useTransactionStore, { transaction } from "../../store/pickUp";
const userStore = useUserInfoStore();
const transactionStore = useTransactionStore();
const showList = ref([])
const loading = ref(false)

const showReverse = ref(false) //冲红弹窗
const reverseInfo = ref<any>({}) //冲红信息
const reverseReason = ref('') //冲红原因
const query = ref({
  headTile: "",
  startDate: "",
  endDate: "",
  billType: "",
})
const params = ref({
  pageNum: 1,
  pageSize: 10
})
const closeReverse = (flag: boolean)=>{
  showReverse.value = false
  reverseInfo.value = {}

  if(flag){
    queryList()
  }
}

//冲红申请
const handleReverse = (item:any) => {
  reverseInfo.value = item
  reverseReason.value = ''
  showReverse.value = true
}
const handleCancel = (item:any) => {
  Taro.showModal({
    title: '提示',
    content: '是否确定取消这张发票？',
    success: async (r)=>{
      if(r.confirm){
        const res: any = await $post(
            `/selfmachine-system/invoice/miniCancel`,
            {ssiId:item.ssiId}
        )
        if(res.data.code === 200){
          Taro.showToast({
            icon: 'success',
            title: '操作成功'
          })
          queryList()
        }
      }
    }
  })
}
const saveReverse = async () => {
  if(!reverseReason.value){
    Taro.showToast({
      icon: 'none',
      title: '请填写冲红原因'
    })
    return
  }
  const res: any = await $post(
      `/selfmachine-system/invoice/redMark`,
      {
        //customerCode: userStore.userInfo.customerCode,
        invoiceNo: reverseInfo.value.invoiceNo,
        redRemark: reverseReason.value
      }
  )
  if(res.data.code === 200){
    Taro.showToast({
      icon: 'success',
      title: '操作成功'
    })
    showReverse.value = false
    queryList()
  }
}

const queryList = async () => {

  const res: any = await $post(
      `/selfmachine-system/invoice/history?pageSize=${params.value.pageSize}&pageNum=${params.value.pageNum}`,
      {
        customerCode: userStore.userInfo.customerCode,
        ...query.value
      }
  )
  if(res.data.code === 200){
    showList.value = showList.value.concat(res.data.rows || [])
    loading.value = false

    if (showList.value.length >= res.data.total) {
      loading.value = true
    }
  }
}
const search = (item) => {
  if(item.start){
    query.value.startDate = item.start + ' 00:00:00'
  }
  else {
    query.value.startDate = ''
  }
  if(item.end){
    query.value.endDate = item.end + ' 23:59:59'
  }
  else {
    query.value.endDate = ''
  }
  query.value.billType = item.flag
  showList.value = []
  queryList()
}


const onLoadList = async()=>{
  params.value.pageNum++;
  queryList();
}

function handleScroll() {
  console.log('到底了')
  if (!loading.value) {
    loading.value = true;
    onLoadList();
  }
}

useDidShow(()=>{
})
queryList()
</script>
<style lang="scss">

.invoiceList {
  height: 100%;
  box-sizing: border-box;
  .lists {
    width: 100%;
    box-sizing: border-box;
    padding: 16rpx 24rpx;
    height: calc(100vh - 212rpx);
    background-color: #f5f5f5;
  }
}
.container_empty {
  margin-top: 280rpx;
  text-align: center;

  image {
    width: 300rpx;
    height: 300rpx;
  }
  .empty_text {
    font-size: 32rpx;
    color: #999;
  }

}
.reverse {
  width: 100%;
  box-sizing: border-box;
  padding: 0 24rpx;
  .nut-textarea {
    padding: 0;
  }
  .reverse_textarea {
    width: 100%;
    background-color: #f5f5f5;
    font-size: 28rpx;
    box-sizing: border-box;
    padding: 12rpx;
    border-radius: 8rpx;
  }
}
.save {
  width: 100%;
  background-color: #fff;
  box-sizing: border-box;
  padding: 32rpx 24rpx 20rpx;
  .btn {
    width: 100%;
    height: 80rpx;
    border-radius: 40rpx;
    background-color: #0966ff;
    color: #fff;
    text-align: center;
    line-height: 80rpx;
  }
}
</style>
